package com.epoch.servicecommunication.service.impl;

import bean.vo.AcceptCom;
import com.epoch.servicecommunication.bean.Communication;
import com.epoch.servicecommunication.dao.CommunicationDao;
import com.epoch.servicecommunication.service.CommunicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import utils.AssertUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author mi
 */
@Service
@Slf4j
public class CommunicationServiceImpl implements CommunicationService {
    @Resource
    private CommunicationDao communicationDao;
    @Override
    public Communication getCommunicationById(String comId, String csId) {
        //非空校验，判断编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(comId),"交流编号不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(csId),"用户编号不能为空！");
        //判断该记录是否存在
        AssertUtil.isTrue(communicationDao.getCommunicationById(comId,csId)==null,"该记录不存在！");
        //返回课程类型
        return communicationDao.getCommunicationById(comId,csId);
    }
    @Override
    public List<Object> getCommunication(String comId) {
        List<Communication> communicationList = communicationDao.getCommunication(comId);
        List<Object> resultList = new ArrayList<>();
        Communication communication;
        for (int i = 0; i < communicationList.size(); i++) {
            HashMap<String,Object> communicationMap = new HashMap(8);
            communication = communicationList.get(i);
            communicationMap.put("id",communication.getId());
            communicationMap.put("csId",communication.getCsId());
            communicationMap.put("csName",communication.getConsumer().getCsName());
            communicationMap.put("comContent",communication.getComContent());
            communicationMap.put("conTime",communication.getConTime());
            resultList.add(communicationMap);
        }
        return resultList;
    }

    @Override
    public void saveCommunication(AcceptCom communication) {
        //非空校验，判断编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(communication.getComId()),"交流编号不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(communication.getCsId()),"用户编号不能为空！");
        //添加交流表，判断受影响的行数
        AssertUtil.isTrue(communicationDao.saveCommunication(communication)<1,"添加失败！");
    }

    @Override
    public void deleteCommunication(Integer id) {
        //非空校验，判断编号是否为空
        AssertUtil.isTrue(id==null,"编号不能为空！");
        //删除交流表，判断受影响的行数
        AssertUtil.isTrue(communicationDao.deleteCommunication(id)<1,"删除失败！");
    }
}
